/**
 * 加载课程可以领取的优惠券
 * @param _courseId 课程ID
 */
function loadCourseCode(_courseId) {
    var couponList = getCourseCouponList(_courseId);
    if(couponList ==null){
        $.toast("没有可领取的优惠券");
        return;
    }
    var selectOption = new Array();
    selectOption.push({
        text: '优惠券',
        label: true
    });
    $.each(couponList,function (index,coupon) {
        var opCardsEle = '';
        opCardsEle +='<div class="cards-yh-bx">';
        opCardsEle +='<div class="row no-gutter" id="165">';
        opCardsEle +='<section class="col-33">';
        opCardsEle +='<div class="text-center Coupon-red-bg">';
        if(coupon.type==1){
            opCardsEle +='<div><span class="fs7">折</span><span class="uAccount-number"><b>'+coupon.limitAtion+'</b></span></div>';
            opCardsEle +='<div><span class="fs6">折扣券</span></div>';
        }else{
            opCardsEle +='<div><span class="fs7">减</span><span class="uAccount-number"><b>'+coupon.limitAtion+'</b></span></div>';
            opCardsEle +='<div><span class="fs6">定额券</span></div>';
        }
        opCardsEle +='</div></section><section class="col-66">';
        opCardsEle +='<div class="pl5">';
        opCardsEle +='<section class="fs7 ellipsis-rows">';
        opCardsEle +='<span class="c-333">消费满</span>';
        opCardsEle +='<span class="c-red">'+coupon.minAmount+'</span>';
        opCardsEle +='<span class="c-333">元时可用</span></section>';
        opCardsEle +='<div class="pt5 row no-gutter dashed">';
        if(coupon.timeType==1){
            opCardsEle +='<aside class="col-66"><span class="fs4 c-888">有效期至<br/>'+coupon.endTime+'</span></aside>';
        }else{
            opCardsEle +='<aside class="col-66"><span class="fs4 c-888">有效期至<br/>无时间限制</span></aside>';
        }

        opCardsEle +='<aside class="col-33">';
        if(coupon.receive==true){//已领取
            opCardsEle +='<a href="javascript:void(0);" class="button button-fill button-round button-success watch-live live-playback">已领取</a>';
        }else{//未领取
            opCardsEle +='<a href="javascript:void(0);" class="button button-fill button-round button-danger fs6">领取</a>';
        }
        opCardsEle +='</aside></div></div></section></div></div>';
        var obj = {
            text: opCardsEle,
            bold: true,
            onClick: function(evnt) {
                //领取优惠券
                couponReceive(coupon.id,coupon.receive);
            }
        };
        selectOption.push(obj);
    });
    var cancelOpBtn = [
        {
            text: '完成',
            bg: 'danger'
        }
    ];
    var opCardGroups = [selectOption, cancelOpBtn];
    $.actions(opCardGroups);
    $('.actions-modal.modal-in').addClass('wm-opCards__wrap');
    $('.actions-modal-button-bold').wrapAll('<div class="wm-opCards-item-wrap"></div>')
}

var receiveMap = {};
/**
 * 领取优惠券
 * @param couponId 优惠券ID
 * @param receive 是否领取状态
 */
function couponReceive(couponId,receive){
    var _receive = receiveMap[couponId];
    if(receive==true || _receive ==true){
        return;
    }
    isLogin(function () {
        $.ajax({
            url: baseloaction + '/mobile/uc/op/coupon/couponReceive',
            type:'post',
            data:{
                'couponIds':(couponId+'')
            },
            success:function (result) {
                $.toast(result.message);
                if(result.success==true){
                    //设置成已领取
                    receiveMap[couponId] = true;
                }
            },
            error:function (error) {
                $.toast("系统错误，领取优惠券失败");
            }
        });
    });
}

/**
 * 获取课程的可以领取的优惠券
 */
function getCourseCouponList(_courseId) {
    var couponList = null;
    $.ajax({
        url: baseloaction +'/mobile/course/getCourseCouponList',
        type:'post',
        dataType:'json',
        async:false,
        data:{
            'courseId':_courseId
        },
        success:function (result) {
            if(result.success==true){
                var list = result.entity;
                if(list !=null && list.length > 0){
                    couponList = result.entity;
                }
            }
        }
    });
    return couponList;
}

/**
 * 获取课程可以使用的活动
 */
function courseInfoActivity(price) {
    $.ajax({
        url: baseloaction +'/mobile/course/getCourseInfoActivity',
        type:'post',
        dataType:'json',
        data:{
            'price':price
        },
        success:function (result) {
            if(result.success==true){
                var list = result.entity;
                if(list !=null && list.length >0){
                    var context = '';
                    $.each(list,function (index,activity) {
                        context +='<section>';
                        context +='<span class="fs6">· '+activity.activityName+'</span>';
                        context +='</section>';
                    });
                    $("#activity-context-box").html(context);
                }
            }
        }
    });
}